package com.ruoyi.web.controller.lc;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.lc.domain.LcDatamodelColumn;
import com.ruoyi.lc.domain.LcDatamodelDefine;
import com.ruoyi.lc.enums.LcDatamodelColumnAttrEnum;
import com.ruoyi.lc.enums.LcDatamodelDefineStateEnum;
import com.ruoyi.lc.service.ILcDatamodelDefineService;
import com.ruoyi.lc.service.ITableService;
import com.ruoyi.lc.util.LcDataModelUtil;

/**
 * 数据建模Controller
 * 
 * @author qibotian
 * @date 2025-01-07
 */
@RestController
@RequestMapping("/lc/datamodel/define")
public class LcDatamodelDefineController extends BaseController {
	@Autowired
	private ILcDatamodelDefineService lcDatamodelDefineService;

	@Autowired
	private ITableService tableService;

	/**
	 * 查询数据建模的所有列
	 */
	@PreAuthorize("@ss.hasPermi('datamodel')")
	@PostMapping("/getLcDatamodelColumn")
	public AjaxResult getLcDatamodelColumn(@RequestBody LcDatamodelColumn column) {
		LcDatamodelDefine define = lcDatamodelDefineService.selectLcDatamodelDefineByCode(column.getDefineCode());
		List<LcDatamodelColumn> columns = new ArrayList<>(0);
		if(LcDatamodelDefineStateEnum.OK.getCode().equals(define.getState())) {
			columns = LcDataModelUtil.findColumnsFromDesc(define.getDesc());
		} else {
			columns = LcDataModelUtil.findColumnsFromDesc(define.getWhzDesc());
		}
		for(LcDatamodelColumn col : columns) {
			if(col.getCode().equals(column.getCode())) {
				return AjaxResult.success(col);
			}
		}
		return AjaxResult.error("找不到的列：" + column.getCode());
	}
	
	/**
	 * 查询数据建模的所有列
	 */
	@PreAuthorize("@ss.hasPermi('datamodel')")
	@PostMapping("/listColumns/{defineCode}")
	public AjaxResult listColumns(@PathVariable String defineCode) {
		LcDatamodelDefine define = lcDatamodelDefineService.selectLcDatamodelDefineByCode(defineCode);
		if (LcDatamodelDefineStateEnum.OK.getCode().equals(define.getState())) {
			return AjaxResult.success(LcDataModelUtil.findColumnsFromDesc(define.getDesc()));
		} else {
			return AjaxResult.success(LcDataModelUtil.findColumnsFromDesc(define.getWhzDesc()));
		}
	}
	
	
	/**
	 * 保存列
	 * @param column
	 * @return
	 */
	@PreAuthorize("@ss.hasPermi('datamodel')")
	@Log(title = "数据建模列", businessType = BusinessType.UPDATE)
	@PostMapping("/saveColumns")
	public AjaxResult saveColumns(@RequestBody LcDatamodelColumn column) {
		if(LcDataModelUtil.isSystemColumn(column)) {
			return error("系统属性不允许修改");
		}
		column.setCode(column.getCode().toUpperCase());
		column.setAttr(LcDatamodelColumnAttrEnum.USER.getCode()); 
		lcDatamodelDefineService.saveColumn(column);
		return AjaxResult.success();
	}
	
	
	/**
	 * 删除列
	 * @param column
	 * @return
	 */
	@PreAuthorize("@ss.hasPermi('datamodel')")
	@Log(title = "数据建模列", businessType = BusinessType.DELETE)
	@PostMapping("/deleteColumnByCode")
	public AjaxResult deleteColumnByCode(@RequestBody LcDatamodelColumn column) {
		lcDatamodelDefineService.deleteColumn(column.getDefineCode(), column.getCode());
		return AjaxResult.success();
	}
	
	
	
	/**
	 * 导出数据建模列表
	 */
	@PreAuthorize("@ss.hasPermi('datamodel')")
	@Log(title = "数据建模", businessType = BusinessType.EXPORT)
	@PostMapping("/exportColumns")
	public void exportColumns(HttpServletResponse response, @RequestBody LcDatamodelDefine lcDatamodelDefine) {
		LcDatamodelDefine define = lcDatamodelDefineService.selectLcDatamodelDefineByCode(lcDatamodelDefine.getCode());
		ExcelUtil<LcDatamodelColumn> util = new ExcelUtil<LcDatamodelColumn>(LcDatamodelColumn.class);
		if (LcDatamodelDefineStateEnum.OK.getCode().equals(define.getState())) {
			util.exportExcel(response, LcDataModelUtil.findColumnsFromDesc(define.getDesc()), "数据建模数据");
		} else {
			util.exportExcel(response, LcDataModelUtil.findColumnsFromDesc(define.getWhzDesc()), "数据建模数据");
		}
	}
	
	
	/**
	 * 维护数据建模列表
	 */
	@PreAuthorize("@ss.hasPermi('datamodel')")
	@PutMapping("/weihu/{id}")
	@Log(title = "数据建模", businessType = BusinessType.UPDATE)
	public AjaxResult weihu(@PathVariable("id") Long id) {
		lcDatamodelDefineService.weihu(id);
		return AjaxResult.success();
	}
	
	
	
	
	/**
	 * 维护数据建模列表
	 */
	@PreAuthorize("@ss.hasPermi('datamodel')")
	@PutMapping("/publish/{id}")
	@Log(title = "数据建模", businessType = BusinessType.UPDATE)
	public AjaxResult publish(@PathVariable("id") Long id) {
		lcDatamodelDefineService.publish(id);
		return AjaxResult.success();
	}

	/**
	 * 查询数据建模列表
	 */
	@PreAuthorize("@ss.hasPermi('datamodel')")
	@GetMapping("/list")
	public TableDataInfo list(LcDatamodelDefine lcDatamodelDefine) {
		startPage();
		List<LcDatamodelDefine> list = lcDatamodelDefineService.selectLcDatamodelDefineList(lcDatamodelDefine);
		for(LcDatamodelDefine define : list) {
			if(!LcDatamodelDefineStateEnum.OK.getCode().equals(define.getState())) {
				define.setTitle(define.getWhzTitle());
			}
		}
		return getDataTable(list);
	}
	

	/**
	 * 导出数据建模列表
	 */
	@PreAuthorize("@ss.hasPermi('datamodel')")
	@Log(title = "数据建模", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	public void export(HttpServletResponse response, LcDatamodelDefine lcDatamodelDefine) {
		List<LcDatamodelDefine> list = lcDatamodelDefineService.selectLcDatamodelDefineList(lcDatamodelDefine);
		for(LcDatamodelDefine define : list) {
			if(!LcDatamodelDefineStateEnum.OK.getCode().equals(define.getState())) {
				define.setTitle(define.getWhzTitle());
			}
		}
		ExcelUtil<LcDatamodelDefine> util = new ExcelUtil<LcDatamodelDefine>(LcDatamodelDefine.class);
		util.exportExcel(response, list, "数据建模数据");
	}

	/**
	 * 获取数据建模详细信息
	 */
	@PreAuthorize("@ss.hasPermi('datamodel')")
	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable("id") Long id) {
		LcDatamodelDefine define = lcDatamodelDefineService.selectLcDatamodelDefineById(id);
		if(!LcDatamodelDefineStateEnum.OK.getCode().equals(define.getState())) {
			define.setTitle(define.getWhzTitle());
		}
		return success(define);
	}
	
	/**
	 * 获取数据建模详细信息
	 */
	@PreAuthorize("@ss.hasPermi('datamodel')")
	@DeleteMapping(value = "/{id}")
	public AjaxResult delete(@PathVariable("id") Long id) {
		lcDatamodelDefineService.deleteLcDatamodelDefineById(id);
		return success();
	}

	/**
	 * 新增数据建模
	 */
	@PreAuthorize("@ss.hasPermi('datamodel')")
	@Log(title = "数据建模", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody LcDatamodelDefine define) {
		// 判断系统中是否已经存在表
		if (tableService.isTableExists(define.getCode())) {
			return error("系统中已经存在表名为" + define.getCode() + "的表，请先验证后创建");
		}
		if (lcDatamodelDefineService.selectLcDatamodelDefineByCode(define.getCode()) != null) {
			return error("已经存在表名为" + define.getCode() + "的定义表，请先验证后创建");
		}
		return toAjax(lcDatamodelDefineService.insertLcDatamodelDefine(define));
	}

	/**
	 * 修改数据建模
	 */
	@PreAuthorize("@ss.hasPermi('datamodel')")
	@Log(title = "数据建模", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody LcDatamodelDefine lcDatamodelDefine) {
		return toAjax(lcDatamodelDefineService.updateLcDatamodelDefine(lcDatamodelDefine));
	}

}
